package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.cache.impl.KiteContextCache;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.pub.client.constanst.Symbols;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.webService.QryIOTOppoDockingSalesRsp;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;


/**
* @Description: 商机查询服务
*
* @author lyx
* @date 2020年11月20日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr
*/
@Service
public class BusinessOpportunitiesAttrHandler extends AbstractAttrHandler{

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(BusinessOpportunitiesAttrHandler.class);

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    IInfServiceBus infServiceBus;

    @Override
    public void init(CommonAttrVO commonAttr) {
        LocalAttrHelper.hidden5GCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr);
        KiteAttr attr = KiteBeanUtils.transform(commonAttr, KiteAttr.class);
        List<KiteAttrValue> attrValues = new ArrayList<>();
        List<StaticAttrDO> staticAttrList = new ArrayList<>();
        List<QryIOTOppoDockingSalesRsp> iotoOppoList = new ArrayList<>();
        try {
            CuccContextCustDTO custDTO = KiteContextCache.get(KiteStringUtils.joinWith(Symbols.AT, KiteWebContext.getKiteContextId(), "custInfo"));
            iotoOppoList = infServiceBus.queryIOTOppoDockingSales(
                KiteObjectUtils.isEmpty(custDTO) || KiteStringUtils.isEmpty(custDTO.getCbssId()) ?
                    KiteWebContext.getContext().getCustId() :
                    custDTO.getCbssId());
        }
        catch (BssException e) {
            LOGGER.error(e.getFailMsg());
            return;
        }
        iotoOppoList.stream().forEach(oppo -> {
            KiteAttrValue attrValue = new KiteAttrValue();
            attrValue.setAttrValue(oppo.getOppoNumber());
            attrValue.setAttrValueName(oppo.getOppoName());
            attrValues.add(attrValue);

            StaticAttrDO staticAttr = new StaticAttrDO();
            staticAttr.setAttrValue(oppo.getOppoNumber());
            staticAttr.setAttrValueDesc(oppo.getOppoName());
            staticAttrList.add(staticAttr);
        });
        attr.setAttrValues(attrValues);
        attr.setModuleId(LocalKeyConsts.GROUPUSERINFO_MODULEID);
        // 五大双线，订购的时候不需要展示商机，资源核查展示商机，后续环节商机不允许修改
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        String cateId = querySceneInstService.getOfferInstAttrValue(KiteWebContext.getContext().getCustId(), sceneInst.getRootInstId(), "cateId");
        String bizType = querySceneInstService.getExtParameterValue(sceneInstId, "bizType");
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");
        if (KiteStringUtils.isEmpty(nodeCode)) {
            nodeCode = "OrderInput";
        }
        if (KiteStringUtils.equalsAny(cateId, ProdCateUtil.getDoubleLineCateIdForArr())) {
            // 订购信息录入
            if (KiteStringUtils.equals(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER) && KiteStringUtils.isEmpty(bizType) && KiteStringUtils.equals(nodeCode, "OrderInput")) {
                commonAttr.setShowType("E");
                commonAttr.setIsEdit("F");
                attr.setShowType("E");
                attr.setIsEdit("F");
            }
            // 资源核查信息录入
            else if (KiteStringUtils.equals(serviceOfferId, CuccServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK) && KiteStringUtils.isEmpty(bizType) && KiteStringUtils.equals(nodeCode, "OrderInput")) {
                commonAttr.setShowType("D");
                commonAttr.setIsEdit("T");
                attr.setShowType("D");
                attr.setIsEdit("T");
            } else {
                commonAttr.setShowType("D");
                commonAttr.setIsEdit("F");
                attr.setShowType("D");
                attr.setIsEdit("F");
            }
        }
        SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), attr);
        commonAttr.setAttrValues(staticAttrList);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isEqual(LocalKeyConsts.BUSINESS_NAME, fieldName) || KiteStringUtils.isEqual(LocalKeyConsts.BUSINESS_NAME_NEW, fieldName)) {
            KiteAttr attr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.BUSINESS, LocalKeyConsts.GROUPUSERINFO_MODULEID);
            if(Objects.isNull(attr)) {
            	 attr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.BUSINESS_NEW, LocalKeyConsts.GROUPUSERINFO_MODULEID);
            }
            if (Objects.nonNull(attr)) {
                // 回填的时候，商机名称value和valueDesc是一样的，当在录入的时候选择商机名称触发联动时，value是商机编码
                if (!KiteStringUtils.equals(value, valueDesc)) {
                    attr.setValue(value);
                    attr.setValueDesc(valueDesc);
                    SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), attr);
                }
            }
        }
        return super.apply(sceneInstId, fieldName, valueDesc, valueDesc);
    }
}
